<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/31
 * Time: 17:53
 */

namespace app\common\lib\task;


use app\common\lib\chuanglan\Sms;
use app\common\lib\common\Redis;
use app\common\lib\common\StringHandle;
use think\App;
use think\Config;
use think\Exception;

class Task
{
    /**
     * @param $data
     * @return string
     * 异步发送短信码
     */
    public function smsTask($data)
    {
        $code = mt_rand(0000, 9999);
        try {
            $smd = new Sms();
            $res = $smd->sendCode($data['tel'], $code, 'login');
            if ((string)$res['code'] === '0') {
                Redis::set(StringHandle::front($data['tel']), $code, StringHandle::time('smsCode'));
                return $res;
            }
            return false;
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }


    /**
     * @param $data
     * @return string
     * 异步删除登录成功清除code
     */
    public function delSmsCode($data)
    {
        try {
           return Redis::del(StringHandle::front($data['tel']));
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

//    /**
//     *  记录所有请求日志
//     */
//    public function writeLog($data){
//        $logs = json_encode($data);
////        异步io写入文件
//        swoole_async_writefile(__DIR__.'/../../../../runtime/log/'.date('Ym').'/'.date('d').'_access.log',$logs.PHP_EOL,function ($filename){
//
//        },FILE_APPEND);
//    }

}